# -*- coding: utf-8 -*-
"""
Created on 2021-09-15 16:33:15
---------
@summary:
---------
@author: xubin
"""

import feapder
from feapder.utils.tools import get_md5

from items import sinobiological_task_info_item


def get_data(node_obj):
    return [_.get() for _ in node_obj]


class CreateTaskForSinoBiological(feapder.AirSpider):
    def start_requests(self):
        url_info = {
            "Recombinant Proteins": [
                "https://www.sinobiological.com/category/fc-receptor-protein",
                "https://www.sinobiological.com/category/igg-fc-control-protein",
                "https://www.sinobiological.com/category/cytokine-protein",
                "https://www.sinobiological.com/category/biomarker-protein",
                "https://www.sinobiological.com/category/kinase-protein",
                "https://www.sinobiological.com/recombinant-proteins/supernuclease-ssnp01",
                "https://www.sinobiological.com/category/protease-protein",
                "https://www.sinobiological.com/category/enzyme-protein",
                "https://www.sinobiological.com/category/fluorescence-protein",
                "https://www.sinobiological.com/category/biological-target-protein",
                "https://www.sinobiological.com/category/diagnostic-protein",
                "https://www.sinobiological.com/category/epigentic-protein",
                "https://www.sinobiological.com/category/cancer-protein",
                "https://www.sinobiological.com/category/immunotherapy-proteins",
                "https://www.sinobiological.com/category/cd-protein",
                "https://www.sinobiological.com/category/receptor-protein",
                "https://www.sinobiological.com/category/immune-checkpoint-protein",
                "https://www.sinobiological.com/category/cell-therapy-protein"
            ],
            "Antibodies": [
                "https://www.sinobiological.com/category/primary-antibodies-list",
                "https://www.sinobiological.com/category/secondary-antibodies-list",
                "https://www.sinobiological.com/category/control-antibodies-list",
                "https://www.sinobiological.com/category/loading-controls",
                "https://www.sinobiological.com/category/tag-antibodies",
                "https://www.sinobiological.com/category/isotyping-kits-list"
            ],
            "Kits": [
                "https://www.sinobiological.com/category/elisa-kit",
                "https://www.sinobiological.com/category/elisa-pair-sets-list",
                "https://www.sinobiological.com/elisa-kits/supernuclease-elisa-kit-kit-ssnp01",
                "https://www.sinobiological.com/other-products/annexin-v-fitc-apoptosis-detection-kit-apk10448-f",
                "https://www.sinobiological.com/category/isotyping-kits-list",
                "https://www.sinobiological.com/category/magnetic-beads-ip-kits-list",
                "https://www.sinobiological.com/category/protein-a-g-l-ip-kits-list",
                "https://www.sinobiological.com/category/tag-antibody-ip-kits-list",
                "https://www.sinobiological.com/category/primary-antibody-ip-kits-list",
                "https://www.sinobiological.com/category/magnetic-separators-list",
                "https://www.sinobiological.com/category/synthetic-peptide-list"
            ],
            "More Reagents": [
                "https://cn.sinobiological.com/category/cell-culture-medium-elite",
                "https://www.sinobiological.com/category/control-vectors-list",
                "https://www.sinobiological.com/category/cdna-clone",
                "https://www.sinobiological.com/category/transfection-reagent-list",
                "https://www.sinobiological.com/category/qpcr-primers",
                "https://www.sinobiological.com/category/cell-lysates-list",
                "https://www.sinobiological.com/category/antibody-purification-list",
                "https://www.sinobiological.com/category/protein-a-g-l-resins-list",
                "https://www.sinobiological.com/category/protein-a-g-l-list",
                "https://www.sinobiological.com/other-products/anti-dykddddk-affinity-resin-101274-mm13-rn",
                "https://www.sinobiological.com/category/protein-modification-list",
                "https://www.sinobiological.com/category/neuraminidase-list",
                "https://www.sinobiological.com/recombinant-proteins/supernuclease-gmp-gmp-ssnp01",
            ]
        }
        for name, url_list in url_info.items():
            for url in url_list:
                yield feapder.Request(url=url, name=name)

    def parse(self, request, response):
        name = request.name
        title = response.xpath('//div[@class="taxonomic_tags"]/text()').get()

        if not title:
            url = response.xpath('//div[@class="screen_mobile_all"]//a/@href').get()
            if url:
                yield feapder.Request(url=url, name=name, callback=self.second_parse)
            else:
                item = sinobiological_task_info_item.SinobiologicalTaskInfoItem()
                item.id = get_md5(title)
                item.type = name
                item.sub_type = title
                item.create_by = 'xubin'
                yield item
        else:
            item = sinobiological_task_info_item.SinobiologicalTaskInfoItem()
            item.id = get_md5(title)
            item.type = name
            item.sub_type = title
            item.create_by = 'xubin'
            yield item

    def second_parse(self, request, response):
        name = request.name
        title = response.xpath('//div[@class="taxonomic_tags"]/text()').get()
        item = sinobiological_task_info_item.SinobiologicalTaskInfoItem()
        item.id = get_md5(title)
        item.type = name
        item.sub_type = title
        item.create_by = 'xubin'
        yield item


if __name__ == "__main__":
    CreateTaskForSinoBiological().start()
